$.fn.serializeObject = function()
{
   var o = {};
   var a = this.serializeArray();
   $.each(a, function() {
       if (o[this.name]) {
           if (!o[this.name].push) {
               o[this.name] = [o[this.name]];
           }
           o[this.name].push(this.value || '');
       } else {
           o[this.name] = this.value || '';
       }
   });
   return o;
};

//Gdt全局对象
var PlatForm = {
	setCookie:function(key,value){
		$.cookie(key, value,{expires:30,path:'/'});
	},
	getCookie:function(key){
		return $.cookie(key);
	},
	getPath:function(){
		//var pathName = window.location.pathname.substring(1);
	    //var webName = pathName == '' ? '' : pathName.substring(0, pathName.indexOf('/'));
	    //return window.location.protocol + '//' + window.location.host + '/'+ webName + '/';
	    //return window.location.protocol + '//' + window.location.host + '/'+ webName;
	    return path;
	},
	/**
	 * 加载html
	 * @param url 地址
	 * @param param 参数
	 * @returns 加载地址的html
	 */
	loadHtml:function(url,param){
		var html = "<span>载入中...</span>";
		$.ajax({
			async:false,
			url:url,
			type:'POST',
			dataType:'text',
			data:param,
			success:function(_html){
				html = _html;
			}
		});
		return html;
	},
	/**
	 * 对象不为空
	 * @param obj
	 * @returns {Boolean}
	 */
	isNoBlank:function(){
		if(obj == null)
			return false;
		if(obj == undefined)
			return false;
		if(obj == "")
			return false;
		return true;
	},
	showMessage:function(rtn){
		jQuery.messager.show({
            title:'系统提示',
            msg:rtn.MESSAGE,
            timeout:3000
       });
	},
	setTheme:function(theme){
		jQuery.ajax({
			type:'POST',
			data:{theme:theme},
			url:this.getPath()+'/modules/system/login/setTheme.do',
			async:true
		});
	}
};




/**
 * 系统管理
 */
PlatForm.System={
	Cache:{
		getBtns:function(btnGId){
			var path = PlatForm.getPath();
			var btns = [];
			jQuery.ajax({
				type:'POST',
				dataType:'json',
				data:{btnGId:btnGId},
				//url:path+'/modules/system/btngroup/getButtons.do',
				url:path+'/modules/system/cache/getButtons.do',
				async:false,
				success:function(data){
					btns = data;
				}
			});
			return btns;
		},
		getToolbar:function(btnGId){
			var btns = PlatForm.System.Cache.getBtns(btnGId);
			var toolbar = [];
			jQuery.each(btns,function(i,n){
				with(n){
					toolbar.push({text:btnName,iconCls:icon,handler:function(){eval(btnEvent);}});
				}
			});
			return toolbar;
		},
		getDictItems:function(dictType){
			var path = PlatForm.getPath();
			var items = [];
			jQuery.ajax({
				type:'POST',
				dataType:'json',
				data:{ecode:dictType},
				url:path+'/modules/system/dicttype/getDict.do',
				async:false,
				success:function(data){
					items = data;
				}
			});
			return items;
		},
		getDictItem:function(dictType,ekey){
			var me = this;
			var items = me.getDictItems(dictType);
			var item = null;
			jQuery.each(items,function(i,n){
				if(n.ekey == ekey){
					item = n;
					return false;
				}
			});
			return item;
		},
		getDictItemsKey:function(dictType,evalue){
			var me = this;
			var items = me.getDictItems(dictType);
			var key = null;
			jQuery.each(items,function(i,n){
				if(n.eval == evalue){
					key = n.ekey;
					return false;
				}
			});
			return key;
		},
		getDictItemsValue:function(dictType,ekey){
			var me = this;
			var items = me.getDictItems(dictType);
			var eval = null;
			jQuery.each(items,function(i,n){
				if(n.ekey == ekey){
					eval = n.eval;
					return false;
				}
			});
			return eval;
		},
		getParam:function(code){
			var param = null;
			var path = PlatForm.getPath();
			var url = path+'/modules/system/paramitem/getParamByCode/'+code+'.do';
			jQuery.ajax({
				type:'POST',
				dataType:'json',
				url:url,
				async:false,
				success:function(data){
					param = data;
				}
			});
			return param;
		}
	}
};

PlatForm.Message={
	show:function(msg){
		var m = window.top.$;
		if(m && m.messager){
			
		}else{
			m=$;
		}
		m.messager.show({
            title:'系统提示',
            msg:msg,
            showType:'slide',
            timeout:1500,
            style:{
                right:'',
                top:document.body.scrollTop+document.documentElement.scrollTop,
                bottom:''
            }
        });
	}
}

/**
 * 工具类
 */
PlatForm.Util={
	TreeUtil:{
		/**
		 * 树菜单 扁平数据转换为层级数据
		 */
		convertToChildrenData:function(opt){
			var i,
			l,
			treeData = [],
			tmpMap = [];
			
			var idFiled = opt.idFiled || 'id';
			var parentField = opt.parentField;
			var data = opt.data;
			
			for (i = 0, l = data.length; i < l; i++) {
				tmpMap[data[i][idFiled]] = data[i];
			}
			
			for (i = 0, l = data.length; i < l; i++) {
				if (tmpMap[data[i][parentField]] && data[i][idFiled] != data[i][parentField]) {
					if (!tmpMap[data[i][parentField]]['children'])
						tmpMap[data[i][parentField]]['children'] = [];
					tmpMap[data[i][parentField]]['children'].push(data[i]);
				} else {
					treeData.push(data[i]);
				}
			}
			return treeData;
		}
	},
	ObjectUtil : {
		clone : function(fromObj) {
			
			if(fromObj.__proto__){
				delete fromObj.__proto__;
			}
			console.log({a:1});
			return $(fromObj).clone();
		}
	}
};

/**
 * 初始化任务
 */
PlatForm.Init={
	initTitle:function(){
		var param = PlatForm.System.Cache.getParam("SYS_TITLE");
		if(param){
			try {
				jQuery("title").text(param.evalue);
			} catch (e) {
				//不兼容处理
				document.title = param.evalue;
			}
		}
	},
	initDictCombobox:function(){
		$(".easyui-combobox[dictType]").each(function(i,n){
			var data = [{eval:'请选择',ekey:'',selected:true}];
			var dataType = $(this).attr("dictType");
			var noPlase = $(this).attr("noPlase");
			var dictItems = $(this).attr("dictItems");
			if(noPlase == 'true'){
				data = [];
			}
			var items = PlatForm.System.Cache.getDictItems(dataType);
			
			
			if(dictItems){
				dictItems = dictItems.split(',');
				$(items).each(function(i,n){
					$(dictItems).each(function(o,m){
						if(n.ekey == m){
							data.push(n);
						}
					});
				});
			}else{
				data = data.concat(items);
			}
			
			$(this).combobox({
				valueField: 'ekey',
        		textField: 'eval',
        		editable:false,
        		data:data
			});
		});
	}
};

jQuery(function(){
	try {
		PlatForm.Init.initTitle();
		PlatForm.Init.initDictCombobox();
	} catch (e) {
	}
});